﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ViSaw
{
    class Log
    {
        public const string LOG_FILE_PATH = "app.log";
        public static Log sharedLogger;
        private StreamWriter logsw;

        public Log(string path)
        {
            logsw = File.AppendText(LOG_FILE_PATH);
            logsw.AutoFlush = true;
            //Console.SetOut(logsw);
        }

        public static Log getInstance()
        {
            if (sharedLogger == null)
            {
                sharedLogger = new Log(LOG_FILE_PATH);
            }
            return sharedLogger;
        }

        public static void initInstance(string logFilePath = LOG_FILE_PATH)
        {
            sharedLogger = new Log(logFilePath);
        }

        public string getTime()
        {
            //string timeStr = new DateTime().ToString();
            return string.Format("{0:yyyy-MM-dd HH:mm:ss.fff}", DateTime.Now);
        }

        public void debug(string text)
        {
            logsw.WriteLine(getTime() + " [DEBUG] " + text);
        }

        public void info(string text)
        {
            logsw.WriteLine(getTime() + " [INFO] " + text);
        }

        public void warn(string text)
        {
            logsw.WriteLine(getTime() + " [WARN] " + text);
        }

        public void error(string text)
        {
            logsw.WriteLine(getTime() + " [ERROR] " + text);
        }

        public void fatal(string text)
        {
            logsw.WriteLine(getTime() + " [FATAL] " + text);
        }

        public static void Debug(string text)
        {
            getInstance().debug(text);
        }

        public static void Info(string text)
        {
            getInstance().info(text);
        }

        public static void Warn(string text)
        {
            getInstance().warn(text);
        }

        public static void Error(string text)
        {
            getInstance().error(text);
        }

        public static void Fatal(string text)
        {
            getInstance().fatal(text);
        }
    }
}
